package org.zhang.shortlink.admin.controller;

import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.zhang.shortlink.admin.common.convention.result.Result;
import org.zhang.shortlink.admin.common.convention.result.Results;
import org.zhang.shortlink.admin.dto.request.ShortLinkGroupSaveReqDTO;
import org.zhang.shortlink.admin.dto.request.ShortLinkGroupSortReqDTO;
import org.zhang.shortlink.admin.dto.request.ShortLinkGroupUpdateReqDTO;
import org.zhang.shortlink.admin.dto.response.ShortLinkGroupRespDTO;
import org.zhang.shortlink.admin.service.GroupService;

import java.util.List;

/**
 * 短链接分组控制层
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/short-link/admin/v1/group")
public class GroupController {

    private final GroupService groupService;

    /**
     * 新增短链接分组
     * @param shortLinkGroupSaveReqDTO
     * @return
     */
    @PostMapping()
    public Result<Void> save(@RequestBody ShortLinkGroupSaveReqDTO shortLinkGroupSaveReqDTO){
        groupService.saveGroup(shortLinkGroupSaveReqDTO.getName());
        return Results.success();
    }

    /**
     * 查询分组集合
     * @return
     */
    @GetMapping()
    public Result<List<ShortLinkGroupRespDTO>> listGroup(){
        return Results.success(groupService.listGroup());
    }

    /**
     * 修改短链接分组
     * @return
     */
    @PutMapping()
    public Result<Void> updateGroup(@RequestBody ShortLinkGroupUpdateReqDTO shortLinkGroupUpdateReqDTO){
        groupService.updateGroup(shortLinkGroupUpdateReqDTO);
        return Results.success();
    }

    /**
     * 删除短链接分组
     * @return
     */
    @DeleteMapping()
    public Result<Void> deleteGroup(@RequestParam String gid){
        groupService.deleteGroup(gid);
        return Results.success();
    }

    /**
     * 排序短链接分组
     * @param requsetParam
     * @return
     */
    @PostMapping("/sort")
    public Result<Void> sortGroup(@RequestBody List<ShortLinkGroupSortReqDTO> requsetParam){
        groupService.sortGroup(requsetParam);
        return Results.success();
    }
 }
